<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalGraphService.php 431 2011-01-05 11:58:18Z steen.rabol $
 * @package Pradoportal.Common
 *
 */

/**
 * GraphService.php
 *
 * Copyright 2008 by Andreas Bulling
 * All rights reserved.
 *
 * Terms of use and modification:
 *
 * You may modify the following source code as you receive it, in any medium,
 * provided that you conspicuously and appropriately retain the above
 * original copyright notice.
 *
 */


Prado::using('Application.Common.3rdParty.jpgraph.src.jpgraph');
Prado::using('Application.Common.3rdParty.jpgraph.src.jpgraph_bar');
Prado::using('Application.Common.3rdParty.jpgraph.src.jpgraph_line');

class PortalGraphService extends TService
{

	private $type;
	private $xdata = null;
	private $ydata1 = null;
	private $ydata2 = null;
	private $ytitle = null;
	private $width = 500;
	private $height = 400;
	private $shadow;

	public function init($config)
	{
		$request = Prado::getApplication()->getRequest();

		if ($request->contains('graph'))
		{
			$this->type = TPropertyValue::ensureString($request['graph']);
		} 
		else
		{
			throw new TConfigurationException('You must specify the type of the graph');
		}

		if ($request->contains('xdata'))
		{
			$this->xdata = explode( ',', TPropertyValue::ensureString($request['xdata']));
		} 
		else
		{
			throw new TConfigurationException('You must specify the x data for the graph');
		}

		if ($request->contains('ydata1'))
		{
			$this->ydata1 = explode( ',', TPropertyValue::ensureString($request['ydata1']));
		} 
		else
		{
			throw new TConfigurationException('You must specify the y data for the graph');
		}

		if ($request->contains('ydata2'))
		{
			$this->ydata2 = explode( ',', TPropertyValue::ensureString($request['ydata2']));
		}

		if ($request->contains('ytitle'))
		{
			$this->ytitle = TPropertyValue::ensureString($request['ytitle']);
		} 
		else
		{
			throw new TConfigurationException('You must specify the y title for the graph.');
		}
	}

	public function getWidth()
	{
		return $this->width;
	}

	public function setWidth($value)
	{
		$this->width = TPropertyValue::ensureInteger($value);
	}

	public function getHeight()
	{
		return $this->height;
	}

	public function setHeight($value)
	{
		$this->height = TPropertyValue::ensureInteger($value);
	}

	public function getShadow()
	{
		return $this->shadow;
	}

	public function setShadow($value)
	{
		$this->shadow = TPropertyValue::ensureBoolean($value);
	}

	public function run() 
	{
	 //PortalUtil::Info("graph...");
		switch( $this->type )
		{
			case 1:
				$graph = $this->createStackedBarGraph($this->xdata, $this->ydata1, $this->ydata2, $this->ytitle);
			break;

			case 2:
				$graph = $this->createLineGraph($this->xdata, $this->ydata1, $this->ytitle);
			break;
		}

		if($graph)
		{
			// Send image to browser
			//header("Content-type: image/png");
			//imagepng($graph->Stroke());
			//imagedestroy($graph);
			$graph->Stroke();
		}
		else
			die("Graph not created");
	}

	private function createStackedBarGraph($xdata, $data1, $data2, $ytitle)
	{
		// Create the graph.
		$graph = new Graph($this->width, $this->height, "auto");
		$graph->SetScale("textlin");
		$graph->title->SetFont(FF_FONT1, FS_BOLD);
		$graph->SetFrame(false);

		if( $this->shadow )
			$graph->SetShadow();

		// Create the bar plots
		$b1plot = new BarPlot($data1);
		$b1plot->SetFillColor("khaki");
		$b1plot->value->Show();
		$b1plot->value->SetFormat('%d');
		$b1plot->value->SetColor("black");
		$b1plot->SetValuePos('center');
		$b1plot->SetLegend(Prado::localize('legend'));

		$b2plot = new BarPlot($data2);
		$b2plot->SetFillColor("orange");
		$b2plot->value->Show();
		$b2plot->value->SetFormat('%d');
		$b2plot->value->SetColor("black");
		$b2plot->SetValuePos('center');
		$b2plot->SetLegend(Prado::localize('legend'));

		// Create the stacked bar plot...
		$gbplot = new AccBarPlot(array($b1plot, $b2plot));

		// ... and add it to the graph
		$graph->Add($gbplot);

		$graph->xaxis->title->Set(Prado::localize('title'));
		$graph->xaxis->SetTickLabels($xdata);
		$graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD);

		$graph->yaxis->title->Set(Prado::localize($ytitle));
		$graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD);
		$graph->yaxis->SetLabelMargin(5);
		$graph->yaxis->scale->SetGrace(10);
		$graph->yaxis->HideZeroLabel();
		$graph->ygrid->SetFill(true,'#f2f2f2@0.5','#cacaca@0.5');

		return $graph;
	}

	private function createLineGraph($xdata, $data1, $ytitle)
	{
		// Create the graph.
		$graph = new Graph($this->width, $this->height, "auto");
		$graph->SetScale("textlin");
		$graph->title->SetFont(FF_FONT1, FS_BOLD);
		$graph->SetFrame(false);

		if( $this->shadow )
			$graph->SetShadow();

		// Create the line plot
		$lineplot = new LinePlot($data1);
		$lineplot->SetColor("orange");
		$lineplot->value->Show();
		$lineplot->value->SetFormat('%d');
		$lineplot->value->SetColor("black");

		// ... and add it to the graph
		$graph->Add($lineplot);

		$graph->xaxis->title->Set(Prado::localize('title'));
		$graph->xaxis->SetTickLabels($xdata);
		$graph->xaxis->title->SetFont(FF_FONT1, FS_BOLD);

		$graph->yaxis->title->Set(Prado::localize($ytitle));
		$graph->yaxis->title->SetFont(FF_FONT1, FS_BOLD);
		$graph->yaxis->SetLabelMargin(5);
		$graph->yaxis->scale->SetGrace(10);
		$graph->yaxis->HideZeroLabel();
		$graph->ygrid->SetFill(true,'#f2f2f2@0.5','#cacaca@0.5');

		return $graph;
	}

	//public function constructUrl($pagePath,$getParams=null,$encodeAmpersand=true,$encodeGetItems=true)
	//{
	//	return parent::constructUrl($pagePath,$getParams,false,$encodeGetItems);
	//}
}
?>
